package com.yzf.smsforward;

import android.content.Context;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.TextView;

import com.yzf.pcsmsforward.R;
import com.yzf.smsforward.utils.Utils;

import java.util.ArrayList;

import static com.yzf.smsforward.Constants.TAG;

/**
 * 项目名称：FoldingVerifyCode
 * 类描述：验证码、银行、手机号码、时间显示适配器
 * 创建人：Administrator
 * 创建时间：2019年10月1日
 */
public class SMSAdapter extends BaseAdapter {

    private Context mContext;
    private ArrayList<Sms> mList;

    SMSAdapter(Context context, ArrayList<Sms> list) {
        mContext = context;
        this.mList = list;
    }

    public void refresh(ArrayList<Sms> list) {
        mList = list;//传入list，然后调用notifyDataSetChanged方法
        for(Sms s : mList){
            Log.d(TAG,  s.getVerifycode() + "~~~~~~~~~~~~~~~~~~~~~~~~~~~~~");
        }
        notifyDataSetInvalidated();
    }


    @Override
    public int getCount() {
        return mList.size();
    }

    @Override
    public Object getItem(int position) {
        return mList.get(position);
    }

    @Override
    public long getItemId(int position) {
        return position;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        ViewHolder mHolder;
        if (convertView == null) {
            mHolder = new ViewHolder();
            LayoutInflater inflater = LayoutInflater.from(mContext);
            convertView = inflater.inflate(R.layout.sms_info, null, true);
            mHolder.mTV_time = convertView.findViewById(R.id.mTV_time);
            mHolder.mTV_send_number = convertView.findViewById(R.id.mTV_send_number);
            mHolder.mTV_receiving_number = convertView.findViewById(R.id.mTV_receiving_number);
            convertView.setTag(mHolder);
            convertView.setBackgroundResource(R.drawable.shape_rectangle_with_radius_list_item_bg);
        } else {
            mHolder = (ViewHolder) convertView.getTag();
        }
       String sms_tel= mList.get(position).getOriginatingAddress();
       String phone_tel= mList.get(position).getReceverPhoneNo();
        if (sms_tel.startsWith("+86")) {
            sms_tel = sms_tel.replace("+86", "");
        }
        if (phone_tel.startsWith("+86")) {
            phone_tel = phone_tel.replace("+86", "");
        }
        mHolder.mTV_time.setText(Utils.stringToLongToDate(mList.get(position).getReceiveTime()));
        mHolder.mTV_send_number.setText(sms_tel);
        String key1 = Constants.numbers.get(Constants.TEL1_KEY);
        if (key1.contains(phone_tel)) {
            mHolder.mTV_receiving_number.setText("卡一");
        } else  {
            mHolder.mTV_receiving_number.setText("卡二");
        }
        return convertView;
    }

    private class ViewHolder {
        private TextView mTV_time;
        private TextView mTV_send_number;
        private TextView mTV_receiving_number;

    }
}
